/*
 *  Licensed to PECATS under one or more contributor license agreements below:
 *  1. http://www.mozilla.org/MPL/MPL-1.1.html
 *  2. http://www.gnu.org/licenses/gpl-2.0.html
 *  3. http://www.gnu.org/licenses/lgpl-2.1.html
*/

/* **********************************************************
*  Class to Read the log file access.log
*  Name: ReadFile.java
*  Author: www.pecats.com
*  Date: Sep 15, 2008
************************************************************/

package com.pecats.batch.core;

import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.util.StringTokenizer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.pecats.batch.data.HitsData;

/**
 * Used to read a log file
 */

public class ReadFile
{
	

	File aFile = null;
	File aDir = null;
	String data = null;
	char identify = 'a';
	StringBuffer sb = null;

	// Variables for handling data within a line
	String ipAddress = null;
	int statusCode = 0;
	String urlName = null;
	String dayTime = null;
	String browser = null;
	
	int year = 0;
	String month = null;
	String day = null;
	int hour = 0;

	int counter=0;
	int urlCounter=0;
	int statusCodeCounter=0;
	int dayCounter=0;	
	int monthCounter=0;
	int yearCounter=0;	
	int browserCounter=0;

	HitsData hd ;

	String nextToken = null;

	BufferedReader input = null;

	
	public ReadFile(File file, File dir)
	{
		System.out.println(System.currentTimeMillis());
		aFile = file;
		aDir = dir;
		readApacheData();
		System.out.println(System.currentTimeMillis());
	}	

	public void readApacheData()
	{
	
		hd = new HitsData();
		try
		{
	
			input = new BufferedReader (new FileReader (aFile));
			String line = null;
		
			while ((line = input.readLine()) != null)
			{
				counter=0;

			// Read the data
				StringTokenizer st = new StringTokenizer(line);
				while (st.hasMoreTokens())
				{
					counter++;
					sb = new StringBuffer(100);
					sb.append(st.nextToken());
					switch(counter)
					{
						/* IP Address */
						case 1:
							ipAddress = sb.toString();
							break;

						/* Idendity of Client */
						case 2:
							break;

						/* Userid of the person */
						case 3:
							break;

						/* Time the request was received (counter 4-5) [24/Feb/2008:19:38:34 +0530] */
						case 4:
							dayTime = sb.toString();
							year = Integer.parseInt(dayTime.substring(8, 12));
							month = dayTime.substring(4, 7);
							day = dayTime.substring(1, 12);
							hour = Integer.parseInt(dayTime.substring(13, 15));
							sb.append(" ");
							sb.append(st.nextToken());
							counter++;
							break;

						case 5:
							break;

						/* URLName being called by the client */
						case 6:
							sb.append(" ");
							urlName = st.nextToken();
							counter++;
							sb.append(urlName);
							sb.append(" ");
							sb.append(st.nextToken());
							counter++;
							break;

						case 7:
							break;
						
						case 8:
							break;

						/* Status Code the server sends back to the client */
						case 9:
							statusCode = Integer.parseInt(sb.toString());
							break;

						/* Size of the object returned to the client */
						case 10:
							break;

						/* Referrer */
						case 11:
							break;

						/* Info the client browser reveals about itself (counter 12-16) */						
						case 12:
							sb.append(" ");
							sb.append(st.nextToken());
							counter++;
							sb.append(" ");
							sb.append(st.nextToken());
							counter++;
							sb.append(" ");
							sb.append(st.nextToken());
							counter++;
							sb.append(" ");
							sb.append(st.nextToken());
							counter++;
							browser = sb.toString().substring(1, sb.length()-1);
							break;
						case 13:
							break;
						case 14:
							break;
						case 15:
							break;
						case 16:
							break;
					}
					
				}

				// Check if the data is present
				
				urlCounter=hd.getUrlIndex(urlName);
				if (urlCounter==-1)
				{
					urlCounter=hd.getSizeUrlName();
					hd.setUrlName(urlCounter, urlName);
				}
				
				statusCodeCounter=hd.getStatusCodesIndex(statusCode);
				if (statusCodeCounter==-1)
				{
					statusCodeCounter=hd.getSizeStatusCodes();
					hd.setStatusCodes(statusCodeCounter, statusCode);
				}
				
				dayCounter=hd.getDaysIndex(day);
				if (dayCounter==-1)
				{
					dayCounter=hd.getSizeDays();
					hd.setDays(dayCounter, day);
				}

				monthCounter=hd.getMonthsIndex(month);
				if (monthCounter==-1)
				{
					monthCounter=hd.getSizeMonths();
					hd.setMonths(monthCounter, month);
				}

				yearCounter=hd.getYearsIndex(year);
				if (yearCounter==-1)
				{
					yearCounter=hd.getSizeYears();
					hd.setYears(yearCounter, year);
				}

				if (counter > 10)
				{
					browserCounter=hd.getBrowsersIndex(browser);
					if (browserCounter==-1)
					{
						browserCounter=hd.getSizeBrowsers();
						hd.setBrowsers(browserCounter, browser);
					}
				}

			// Store the Read Data

				// URL Wise
				hd.setHitsData(urlName, day, hour);
				
				// Hourwise
				hd.setHourHitsData(urlName, hour);

				// Daywise
				hd.setDayHitsData(urlName, day);

				// Monthwise
				hd.setMonthHitsData(urlName, month);

				// Yearwise
				hd.setYearHitsData(urlName, year);
				
				// Set Status Codes Data
				hd.setStatusCodesData(urlName, statusCode);						

				if (counter > 10)
				{
					// Set Browser Data
					hd.setBrowsersData(browser);						
				}
			}

			// Print the Read Data
			PrintData pd = new PrintData(hd);
			pd.printXML(aFile, aDir);
			pd.printHTML(new File(aFile.getName() + ".xml"), aDir);
			pd.printMainHTML(aFile, aDir);
			if (counter > 10)
			{
				PrintData.apacheextended = "Y";
			}


			// Send Mail
			Sendmail sendMail = new Sendmail();
			sendMail.send(aFile, aDir);
		}
		catch	(FileNotFoundException ex)
		{
		}	
		catch 	(IOException ex)
		{
		}
		catch 	( TransformerConfigurationException tfce)
		{
		}
		catch 	(TransformerException tfe)
		{
		}
		catch (Exception e)
		{
		}

		finally
		{
			try
			{
				if (input != null)
				{
					input.close();
				}
			}
			catch (IOException ex)
			{				
			}
		}
	}

	
}